#! /usr/bin/env python
from .isa_base import *

class IALU(ISA_BASE):

    def __init__(self):
        super(IALU, self).__init__()
        name = "%s_instructions.xml"%(self.__class__.__name__.lower())
        self.setFile(name)

    def iadd(self):
        pass

    def imad(self):
        pass

    def idap(self):
       #self.logger.info("parse ISA GROUP : %s opcode : idap"%(self.__class__.__name__))
       opcode = [
        "IDP_RRR",
        "IDP_RRI",
        "IDP_RIR",
        "IDP_RRC",
        "IDP_RCR",
        "IDP_RRU",
        "IDP_RUR",
       ]
       dim = [
          ".2A",
          ".4A",
       ]

       lohi = [
          ".LO",
          ".HI",
       ] 

       afmt = [
        ".S8",
        ".U8",
        ".S16",
        ".U16",
       ]

       bfmt = [
        ".S8",
        ".U8",
       ]

       instrcode = '  <O name="const_bits" type="Constant" bits="15-13,1-0" value="01100"/>'

       for l in lohi:
         for o in opcode:
           for d in dim:
             for a in afmt:
               for b in bfmt:
                  instrution =''
                  operandlist=[]
                  operandlist.append(instrcode)
                  asm=''

                  if l =='.LO':
                    sub_instr = "%s%s%s%s"%(o, d, a, b) 
                  elif l =='.HI':
                    sub_instr = "%s%s%s%s%s"%(o, d, l, a, b) 

                  valid = True

                  SrcBClass = {
                    "GPR" :   'class="RegisterOperand"',
                    "Immediate": 'class="ImmediateOperand"',
                    "Cmem" :   'class="RegisterOperand"',
                    "UR" :   'class="RegisterOperand"',
                  }

                  SrcCClass = {
                    "GPR" :   'class="SignedRegisterOperand"',
                    "Immediate" :'class="SignedImmediateOperand"',
                    "Cmem" :   'class="SignedRegisterOperand"',
                    "UR" :   'class="SignedRegisterOperand"',
                  }

                  match  sub_instr:
                    case "IDP_RRR.2A.S16.S8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.S16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.S16.S8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.S16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.S16.S8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.S16.S8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.S16.S8":
                        SrcBType= "UR"
                        SrcCType= "GPR"
                    case "IDP_RRR.2A.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.S16.U8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.S16.U8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.S16.U8":
                        SrcBType= "UR"
                        SrcCType= "GPR"

                    case "IDP_RRR.2A.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.U16.S8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.U16.S8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.U16.S8":
                        SrcBType= "UR"
                        SrcCType= "GPR"
                    case "IDP_RRR.2A.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.U16.U8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.U16.U8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.U16.U8":
                        SrcBType= "UR"
                        SrcCType= "GPR"

                    case "IDP_RRR.2A.HI.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.HI.S16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.HI.S16.S8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.HI.S16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.HI.S16.S8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.HI.S16.S8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.HI.S16.S8":
                        SrcBType= "UR"
                        SrcCType= "GPR"
                    case "IDP_RRR.2A.HI.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.HI.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.HI.S16.U8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.HI.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.HI.S16.U8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.HI.S16.U8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.HI.S16.U8":
                        SrcBType= "UR"
                        SrcCType= "GPR"

                    case "IDP_RRR.2A.HI.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.HI.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.HI.U16.S8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.HI.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.HI.U16.S8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.HI.U16.S8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.HI.U16.S8":
                        SrcBType= "UR"
                        SrcCType= "GPR"
                    case "IDP_RRR.2A.HI.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.2A.HI.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.2A.HI.U16.U8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.2A.HI.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.2A.HI.U16.U8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.2A.HI.U16.U8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.2A.HI.U16.U8":
                        SrcBType= "UR"
                        SrcCType= "GPR"

                    case "IDP_RRR.4A.S8.S8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.4A.S8.S8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.4A.S8.S8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.4A.S8.S8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.4A.S8.S8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.4A.S8.S8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.4A.S8.S8":
                        SrcBType= "UR"
                        SrcCType= "GPR"
                    case "IDP_RRR.4A.S8.U8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.4A.S8.U8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.4A.S8.U8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.4A.S8.U8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.4A.S8.U8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.4A.S8.U8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.4A.S8.U8":
                        SrcBType= "UR"
                        SrcCType= "GPR"

                    case "IDP_RRR.4A.U8.S8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.4A.U8.S8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.4A.U8.S8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.4A.U8.S8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.4A.U8.S8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.4A.U8.S8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.4A.U8.S8":
                        SrcBType= "UR"
                        SrcCType= "GPR"
                    case "IDP_RRR.4A.U8.U8":
                        SrcBType= "GPR"
                        SrcCType= "GPR"
                    case "IDP_RRI.4A.U8.U8":
                        SrcBType= "GPR"
                        SrcCType= "Immediate"
                    case "IDP_RIR.4A.U8.U8":
                        SrcBType= "Immediate"
                        SrcCType= "GPR"
                    case "IDP_RRC.4A.U8.U8":
                        SrcBType= "GPR"
                        SrcCType= "Cmem"
                    case "IDP_RCR.4A.U8.U8":
                        SrcBType= "Cmem"
                        SrcCType= "GPR"
                    case "IDP_RRU.4A.U8.U8":
                        SrcBType= "GPR"
                        SrcCType= "UR"
                    case "IDP_RUR.4A.U8.U8":
                        SrcBType= "UR"
                        SrcCType= "GPR"

                    case _: 
                        valid = False

                  form = 'form="%s%s%s%s" '%(d.replace(".", ""), l.replace(".", ""), a.replace(".", ""), b.replace(".", ""),)
                  if valid:
                     instrution = '<I name="%s" %sisa="CALM" group="General" extension="IALU">'%(o, form)
                     operand = ' <O name="Rd" type="GPR" bits="23-8" access="Write" class="RegisterOperand" choices="GPRs"/>'
                     operandlist.append(operand)
                     operand = ' <O name="Ra" type="GPR" bits="23-8" access="ReadWrite" choices="GPRs"/>'
                     operandlist.append(operand)
                     operand = ' <O name="SrcB" type="%s" bits="39-8" access="ReadWrite" class="SignedRegisterOperand" choices="GPRs"/>'%(SrcBType, SrcBClass[SrcBtype])
                     operandlist.append(operand)
                     operand = ' <O name="SrcC" type="%s" bits="39-8" access="ReadWrite"  class="SignedRegisterOperand" choices="GPRs"/>'%(SrcCType, SrcCClass[SrcCtype])
                     operandlist.append(operand)
                     operand = ' <O name="sched" type="Immediate" bits="124-121" class="ImmediateOperand" choice="Stall Count"/>'
                     operandlist.append(operand)
                     operand = ' <O name="req" type="ReqSB" bits="117-112" access="Read" class="ReadScoreboardOperand" choices="ScoreBoard"/>'
                     operandlist.append(operand)
                     asm = ' <asm format="%s '%(sub_instr)
                     asm += r'%s, %s, %s, %s, %s, %s %s %s;" op1="Rd" op2="pu" op3="Ra" op4="SrcB" op5="SrcC" op6="pp" op7="sched" op8="req"/>'
                     end = '</I>'

                     code = "%s\n"%(instrution)
                     for i in operandlist:
                        code += "%s\n"%(i)
                     code += "%s\n"%(asm)

                     code += end

                     self.logger.info(code)


                     





        